package cn.stylefeng.guns.modular.video.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.video.entity.VideoFile;
import cn.stylefeng.guns.modular.video.mapper.VideoFileMapper;
import cn.stylefeng.guns.modular.video.model.params.VideoFileParam;
import cn.stylefeng.guns.modular.video.model.result.VideoFileResult;
import  cn.stylefeng.guns.modular.video.service.VideoFileService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 视频内容 服务实现类
 * </p>
 *
 * @author CHENCHEN
 * @since 2022-07-26
 */
@Service
public class VideoFileServiceImpl extends ServiceImpl<VideoFileMapper, VideoFile> implements VideoFileService {

    @Override
    public void add(VideoFileParam param){
        VideoFile entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(VideoFileParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(VideoFileParam param){
        VideoFile oldEntity = getOldEntity(param);
        VideoFile newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public VideoFileResult findBySpec(VideoFileParam param){
        return null;
    }

    @Override
    public List<VideoFileResult> findListBySpec(VideoFileParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(VideoFileParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Long insertVideoFile(VideoFileParam param) {
        VideoFile entity = getEntity(param);
        boolean b = this.save(entity);
        if(b){
            System.out.println(entity.toString());
            return entity.getId();
        }
        return null;
    }

    private Serializable getKey(VideoFileParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private VideoFile getOldEntity(VideoFileParam param) {
        return this.getById(getKey(param));
    }

    private VideoFile getEntity(VideoFileParam param) {
        VideoFile entity = new VideoFile();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
